<template>
  <Modal v-model="modal" title="奖励积分">
    <Form :label-width="100" ref="form" :model="form" :rules="rules">
      <FormItem label="门店名称">
        <p>{{form.storeName}}</p>
      </FormItem>
      <FormItem label="门店类型">
        <p>{{form.storeTypeName}}</p>
      </FormItem>
      <FormItem label="经销商">
        <p>{{form.distributorName}}</p>
      </FormItem>
      <FormItem label="奖励返利" prop="rebate">
        <Input v-model="form.rebate" v-show="false"/>
        <InputNumber v-model="form.rewardRebate" :precision="0" :min="-1000000" :max="1000000" style="width:120px"></InputNumber>
      </FormItem>
      <FormItem label="原因">
        <Input v-model="form.remark" type="textarea" :rows="3" :maxlength="50"/>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button class="mr" size="large" type="text" @click="closeModal">取消</Button>
      <Button type="primary" size="large" :disabled="disabled" @click="onSave">确定</Button>
    </div>
  </Modal>
</template>
<script>
export default {
  data() {
    return {
      modal: false,
      disabled: false,
      form: {
        storeContractId: null,
        storeId: null,
        storeName: '',
        storeTypeName: '',
        distributorName: '',
        rewardRebate: null,
        rebate: '',
        remark: ''
      },
      rules: {
        rebate: [{required: true, message: "奖励返利在负100万到100万之间", trigger: "blur"}]
      }
    }
  },
  methods: {
    updataForm(form) {
      this.$refs.form.resetFields()
      this.form = {
        storeContractId: form.id,
        storeId: form.storeId,
        storeName: form.storeName,
        storeTypeName: form.storeTypeName,
        distributorName: form.distributorName,
        rewardRebate: null,
        rebate: '',
        remark: ''
      }
      this.disabled = false
      this.openModal()
    },
    openModal() {
      this.modal = true;
    },
    closeModal() {
      this.modal = false
    },
    onSave() {
      if (this.disabled) return
      this.disabled = true
      this.$refs["form"].validate(valid => {
        let param = {
          storeContractId: this.form.id,
          storeId: this.form.storeId,
          rewardRebate: this.form.rewardRebate,
          remark: this.form.remark
        }
        if (valid) {
          this.$jsonPost(`/assets/reward_rebate/reward`, this.form).then(data => {
            this.closeModal()
            this.$Notice.success({
              title: '奖励返利操作成功'
            })
            this.$emit('onComplete')
          }).catch(() => {
            this.closeModal()
            this.disabled = false
          })
        } else {
          this.disabled = false
        }
      })
    }
  },
  watch: {
    'form.rewardRebate'(v) {
      if (v == 0 || v) {
        this.form.rebate = v + ''
      } else {
        this.form.rebate = ''
      }
    }
  }
}
</script>
